<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AJAX POST</title>
    <style>
        #result{
            width: 200px;
            height: 200px;
            border: solid 2px red;
        }
    </style>
</head>
<body>

    <div id="result"></div>

    

    <script>
        let div = document.getElementById('result')
        div.addEventListener('mouseover',function(){
            // console.log('test')
            let xhr = new XMLHttpRequest();
            
            xhr.open("POST","http://localhost:8000/server")
            /**
             * Post请求设置请求参数在，xhr.send()方法中添加param
             * 
             * xhr.send('a=10&b=20')
             * 
             */
             //设置请求头，必须要在 xhr.send（）之前
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
            //自定义头信息 => 安全机制  => 后端设置 Access-Control-Allow-Headers,并且还有一个OPTIONS方式请求
            xhr.setRequestHeader('name','jack')
            //XMLHttpRequest() POST方法传参，在send()方法中传入
            xhr.send("a=10&b=20")
           

            xhr.onreadystatechange = function(){
                if(xhr.readyState == 4){
                    if(xhr.status >=200 && xhr.status <300){
                        div.innerHTML = xhr.response
                    }
                }
            }
        })














        
    </script>
   
</body>
</html>